package com.chat.datea.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chat.datea.R;
import com.chat.datea.bean.RanKingEntity;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by dllo on 15/10/20.
 */
public class RanKingAdapter extends BaseAdapter {
    private RanKingEntity kingEntity;
    private List<RanKingEntity.ContentEntity> content;
    private Context mContext;

    public RanKingAdapter(Context c, RanKingEntity entity) {
        mContext = c;
        kingEntity = entity;
        content = kingEntity.getContent();
    }


    @Override
    public int getCount() {
        return content != null && content.size() > 0 ? content.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return content != null && content.size() > 0 ? content.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if (convertView == null) {
            //初始化缓存组件
            holder = new ViewHolder();
            //通过布局加载器加载行布局
            convertView = LayoutInflater.from(mContext).inflate(R.layout.ranking_lsitview_item, null);
            holder.imageView = (ImageView) convertView.findViewById(R.id.ranKing_Image);
            holder.ranKing_Title = (TextView) convertView.findViewById(R.id.ranKing_Title);
            holder.ranKing_Tv = (TextView) convertView.findViewById(R.id.ranKing_Tv);
            holder.ranKing_TvOne = (TextView) convertView.findViewById(R.id.ranKing_TvOne);
            holder.ranKing_TvTwo = (TextView) convertView.findViewById(R.id.ranKing_TvTwo);
            holder.author = (TextView) convertView.findViewById(R.id.author);
            holder.author_One = (TextView) convertView.findViewById(R.id.author_One);
            holder.author_Two = (TextView) convertView.findViewById(R.id.author_Two);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /**
         * 设置组件
         * **/
        //设置标题
        holder.ranKing_Title.setText(content.get(position).getName());
        //设置歌曲名
        holder.ranKing_Tv.setText(content.get(position)
                .getContent().get(1).getTitle());
        //设置歌曲作者
        holder.author.setText(content.get(position)
                .getContent().get(1).getAuthor());

        holder.ranKing_TvOne.setText(content.get(position)
                .getContent().get(2).getTitle());
        holder.author_One.setText(content.get(position)
                .getContent().get(2).getAuthor());

        holder.ranKing_TvTwo.setText(content.get(position)
                .getContent().get(3).getTitle());
        holder.author_Two.setText(content.get(position)
                .getContent().get(3).getAuthor());
        //加载图片
        String url = content.get(position).getPic_s192();
        Picasso.with(mContext).load(url).placeholder(R.mipmap.default_playlist_list).into(holder.imageView);

        return convertView;
    }

    public class ViewHolder {
        public ImageView imageView;
        public TextView ranKing_Title, ranKing_Tv, ranKing_TvOne, ranKing_TvTwo,
                author, author_One, author_Two;

    }
}
